package de.vocabulary.server;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.vocabulary.client.service.VocabularyService;
import de.vocabulary.server.language.Language;
import de.vocabulary.server.language.translation.TSVLookup;
import de.vocabulary.server.language.translation.TranslationLookup;
import de.vocabulary.server.persistence.BerkeleyDbDatabaseSerialBinding;
import de.vocabulary.server.persistence.BerkeleyDbEnvironment;
import de.vocabulary.shared.model.LanguageModule;
import de.vocabulary.shared.model.Vocable;

public class VocabularyServiceImpl extends RemoteServiceServlet implements VocabularyService{

	private static final long serialVersionUID = 1L;
	
	
	
	private SessionDetails sessionDetails;
	private VocableDatabaseWrapper vocableDatabase;
	
	public VocabularyServiceImpl() {

		vocableDatabase = VocableDatabaseWrapper.getVocableDatabaseWrapper(); 
		sessionDetails = SessionDetails.getSessionDetails();
		
	}

	@Override
	public List<Vocable> getVocabularyList() throws Exception {
		
		return vocableDatabase.getVocabularyList();
	}

	@Override
	public void storeVocabularyList(List<Vocable> vocabulary) throws Exception {
		vocableDatabase.storeVocabularyList(vocabulary);
	}

	@Override
	public List<List<String>> getTranslations(List<Vocable> vocabulary) throws Exception {
		List<List<String>> translations = new ArrayList<List<String>>();
		for (Vocable v:vocabulary){
			//if (!v.getLanguage2Word().equals(""))
			//			translations.add(null);
			//	else{
				translations.add(getTranslation(v.getLanguage1Word()));
		}
		return translations;
	}


	@Override
	public List<String> getTranslation(String wordToBeTranslated) throws Exception {

		TranslationLookup lookup = new TSVLookup();
		
		Set<String> translations = lookup.getTranslation(wordToBeTranslated, Language.ISO639_DEU, Language.ISO639_HEB);
		
		return new ArrayList<String>(translations);
	}

	// TODO implement (only names relevant)
	@Override
	public List<String> getLanguages() throws Exception {
		return vocableDatabase.getLanguages();
	}

	@Override
	public LanguageModule getLanguageModule(String name) throws Exception {
		// TODO load database
		int test = 3/5;
		System.out.println("TEST: "+test);
		return new LanguageModule();
	}
	
	
	@Override
	public LanguageModule getDefaultLanguageModule() throws Exception {
		return sessionDetails.getLanguageModule();
	}

	@Override
	public List<String> getLessons() throws Exception {
		return vocableDatabase.getLessons();
	}

	@Override
	public Vocable setParametersAndGetFirstVocable(String lesson, int amountVocabulary)
			throws Exception {
		sessionDetails.setLesson(lesson);
		sessionDetails.setAmountVocabulary(amountVocabulary);
		return sessionDetails.getFirstVocable();
		
	}

	@Override
	public Vocable submitFeedbackAndGetNextVocable(boolean correct,
			boolean ignore) throws Exception {
		sessionDetails.setFeedback(correct, ignore);
		return sessionDetails.getNextVocable();
		
	}

}
